package com.servicesystem.demo.entity;

import com.alibaba.fastjson.JSONObject;
import com.mike.module.core.entity.ElementEntity;
import com.mike.module.core.utils.SpringContextUtil;
import com.mike.module.datas.mapperImpl.CommonMapperImpl;
import com.mike.module.parse.cache.XmlDataCache;
import com.servicesystem.demo.utils.AuthorityUtil;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName ServiceData
 * @Description TODO
 * @Author Mike
 * @Date 2019/6/18 20:32
 * @Version 1.0
 */
@Data
public class ServiceData {
    private Head head;
    private JSONObject body;
    private String getServiceNameSql;

    @Data
    public class Head{
        private String apiName = "";
        private String hostName = "";
        private String time = "";
        private String tagName = "";
        private String path = "";
        private String token = "";
        private String serviceName = "";
        private String model = "prod";
        private String serial = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + String.valueOf(new Random().nextInt(6));
    }

    public Boolean checkApiName(){
        if(head.apiName != null){
            //获取 serviceName
            if(head.apiName.matches("\\w{1,50}")){
                getServiceName();
                return true;
            }
        }
        return false;
    }

    public Boolean checkServiceName(){
        return head.serviceName != null;
    }

    public Boolean checkAuthority(){
        if(head.path.equals("system")){
            return true;
        }else{
            if(head.tagName.matches("\\w{1,50}")){
                return AuthorityUtil.checkUserAuthority(head.tagName,head.token,head.apiName,head.time);
            }
        }
        return false;
    }

    public Boolean checkServiceAuthority(){
        return AuthorityUtil.checkServiceAuthority(head.tagName,head.apiName);
    }

    public List<ElementEntity> getWorkList(){
//        System.out.println(head.serviceName);
        XmlDataCache xmlDataCache = SpringContextUtil.getBean("xmlDataCache");
        if(head.model.equals("prod")){
            return xmlDataCache.getXmlData(head.serviceName);
        }else{
            return xmlDataCache.updateXmlData(head.serviceName);
        }
    }

    public Map<String,Object> getHeadMap(){
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("ApiName",head.apiName);
        map.put("HostName",head.hostName);
        map.put("Time",head.time);
        map.put("TagName",head.tagName);
        map.put("Path",head.path);
        map.put("StartTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
        return map;
    }
    public Map<String,Object> getBodyMap(){
        if(body != null){
            return body.getInnerMap();
        }
        return new ConcurrentHashMap<>();
    }
    //
    private void getServiceName(){
        CommonMapperImpl commonMapper = SpringContextUtil.getBean("commonMapperImpl");
        String sqlVal = this.getServiceNameSql + "'" + this.head.apiName + "'";
        String serviceName = (String) commonMapper.selectOneObject(sqlVal);
        if(serviceName != null){
            this.head.setServiceName(serviceName);
        }
    }
}
